package com.memorypalace.deck.biz;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.memorypalace.deck.mapper.CardMapper;
import com.memorypalace.deck.mapper.NoteMapper;
import com.memorypalace.pojo.Card;
import com.memorypalace.pojo.Note;
import com.memorypalace.pojo.StudyStatus;

@Service("cardBiz")
public class CardBizImpl implements ICardBiz {

	@Autowired
	private CardMapper cardMapper;
	@Autowired
	private NoteMapper noteMapper;

	@Override
	public List<Card> getCardInWrongCount(String userId) {
//		List<Card> difficultCards = new ArrayList<Card>();
		List<Card> cardList = cardMapper.getCardWithCardStyleInWrongCount(userId);
		for (Card card : cardList) {
			Note note = noteMapper.getNoteWithDeckAndNoteTypeById(card.getNoteId());
			card.setNote(note);
		}
		return cardList;
	}

	@Override
	public StudyStatus getCardStudyRecordInToday(String userId) {
		StudyStatus studyStatus = new StudyStatus();
		studyStatus.setStudyNew(cardMapper.getStudyNewCountInToday(userId));
		studyStatus.setStudyReview(cardMapper.getStudyReviewCountInToday(userId));
		studyStatus.setSpendTime(cardMapper.getSpendTimeSumInToday(userId));
		return studyStatus;
	}
	/**
	 * 如果下标为0表示是昨天
	 */
	@Override
	public List<StudyStatus> getCardStudyRecordInThirtyDays(String userId) {
		List<StudyStatus> studyNewList = cardMapper.getStudyNewCountInThirtyDays(userId);
		List<StudyStatus> studyReviewList = cardMapper.getStudyReviewCountInThirtyDays(userId);
		List<StudyStatus> spendTimeList = cardMapper.getSpendTimeSumInThirtyDays(userId);
		List<StudyStatus> result = new ArrayList<StudyStatus>(30);
		for (int i = 0; i < 30; i++) {
			StudyStatus studyStatus = new StudyStatus();
			studyStatus.setStudyTimeDiff(i+1);
			result.add(studyStatus);
			
		}
		// 新的
		for (StudyStatus studyStatus : studyNewList) {
			Integer timeDiff = studyStatus.getStudyTimeDiff();
			StudyStatus s = result.get(timeDiff-1);
			s.setStudyNew(studyStatus.getStudyNew());
			result.remove(timeDiff-1);
			result.add(timeDiff-1, s);
		}
		// 旧的
		for (StudyStatus studyStatus : studyReviewList) {
			Integer timeDiff = studyStatus.getStudyTimeDiff();
			StudyStatus s = result.get(timeDiff-1);
			s.setStudyReview(studyStatus.getStudyReview());
			result.remove(timeDiff-1);
			result.add(timeDiff-1, s);
		}
		// 时间
		for (StudyStatus studyStatus : spendTimeList) {
			Integer timeDiff = studyStatus.getStudyTimeDiff();
			StudyStatus s = result.get(timeDiff-1);
			s.setSpendTime(studyStatus.getSpendTime());
			result.remove(timeDiff-1);
			result.add(timeDiff-1, s);
		}
		return result;
	}
	// if(!map.contains(day))(map.put(day, new ArrayList()))
}
